/**
 * 
 */
package org.xelnaga.defiler.domain;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

/**
 * @author qijb
 *
 */
public class ContentletWrapper {
    private Long inode;
	private String title;
	private String friendlyName;
	private Boolean live;
	private Float salePrice = new Float(34);
	private Float listPrice = new Float(45);
	private Integer soldOut = 15;//卖出数量
	private Float rating = new Float(3);//评分
	private Integer commentCount = 120;//评论数
	private Integer ratingCount = 30;//评分数
	private FileAssetModel fileAsset;//默认图片
	private String content;
	private Long folderInode;
	private CompanyModel company;
	private List<FieldWrapper> fieldWrappers = new ArrayList<FieldWrapper>();

	private List<ContentPriceModel> priceList;
	
	private Map<String, Object> map = new HashMap<String, Object>();
	/**
	 * 
	 */
	public ContentletWrapper(List<FieldModel> fields, ContentletModel contentlet) {
		this.title = contentlet.getTitle();
		this.inode = contentlet.getInode();
		this.friendlyName = contentlet.getFriendlyName();
		this.salePrice = contentlet.getSalePrice();
		this.listPrice = contentlet.getSalePrice();
		this.fileAsset = contentlet.getFileAsset();
		this.content = contentlet.getContent();
		this.folderInode = contentlet.getFolderInode();
		this.company = contentlet.getCompany();
		if(null != fields && null != contentlet) {
			for(FieldModel field : fields) {
				String property = field.getFieldContentlet();
				if(StringUtils.isNotBlank(property)) {
					try {
						Object value = null;
						if(property.contains("_")) {
							String[] array = property.split("_");
							if(array.length == 2) {//handl text_area1
								property = array[0] + StringUtils.capitalize(array[1]);
							}
						}
						value = BeanUtils.getProperty(contentlet, property);
						this.put(field.getVelocityVarName(), value);
						fieldWrappers.add(new FieldWrapper(field, value));
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public void put(String velocityVarName, Object value) {
		map.put(velocityVarName, value);
	}

	public Long getInode() {
		return inode;
	}

	public String getTitle() {
		return title;
	}

	public String getFriendlyName() {
		return friendlyName;
	}

	public Boolean getLive() {
		return live;
	}

	public List<FieldWrapper> getFieldWrappers() {
		return fieldWrappers;
	}

	public Object get(String key) {
		return this.map.get(key);
	}

	public Float getSalePrice() {
		return salePrice;
	}

	public Float getListPrice() {
		return listPrice;
	}

	public FileAssetModel getFileAsset() {
		return fileAsset;
	}

	public Integer getSoldOut() {
		return soldOut;
	}

	public void setSoldOut(Integer soldOut) {
		this.soldOut = soldOut;
	}

	public Float getRating() {
		return rating;
	}

	public void setRating(Float rating) {
		this.rating = rating;
	}

	public Integer getCommentCount() {
		return commentCount;
	}

	public void setCommentCount(Integer commentCount) {
		this.commentCount = commentCount;
	}

	public Integer getRatingCount() {
		return ratingCount;
	}

	public void setRatingCount(Integer ratingCount) {
		this.ratingCount = ratingCount;
	}

	public List<ContentPriceModel> getPriceList() {
		return priceList;
	}

	public void setPriceList(List<ContentPriceModel> priceList) {
		this.priceList = priceList;
	}

	public String getContent() {
		return content;
	}

	public Long getFolderInode() {
		return folderInode;
	}

	public CompanyModel getCompany() {
		return company;
	}
	
}
